package com.ztgf.shopping.service.shop;

import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.user.models.dto.UserRoleRelationDTO;
import com.ztgf.user.models.service.IUserRoleRelationService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopUserRelationMapper;
import com.ztgf.shopping.models.service.shop.IShopUserRelationService;
import com.ztgf.shopping.models.bo.shop.ShopUserRelationSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopUserRelationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;

import java.util.List;

/**
 * 店员表控制器
 *
 * @author zhanghualang
 * @Date 2020-05-28 14:43:23
 */
@DubboService
public class ShopUserRelationServiceImpl extends ServiceImpl<ShopUserRelationMapper, ShopUserRelationDTO> implements IShopUserRelationService{

    @Resource
    private YmlConfig ymlConfig;
    @Autowired
    @Lazy
    private IShopInfoService iShopInfoService;
    @DubboReference
    private IUserRoleRelationService iUserRoleRelationService;

    @Override
    public boolean add(ShopUserRelationSaveBO saveBO) {
        ShopUserRelationDTO dto = new ShopUserRelationDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }
    @Override
    public Long getRoleByShop(Long id) {
        ShopInfoDTO shopInfoDTO = iShopInfoService.selectById(id);
        List<UserRoleRelationDTO> userRoleRelationDTOList = iUserRoleRelationService.queryListByUserId(shopInfoDTO.getUserId());
        if (userRoleRelationDTOList.size() <= 0){
            return 0l; //若无权限，返回0
        }
        return userRoleRelationDTOList.get(0).getRoleId();
    }
}
